'use strict';

const _ = require('lodash');
const fs = require('fs-extra');
const request = require('request-promise-native');

const BASE_URL = 'http://121.199.72.208:8081/SGHWeb/AjaxHandler.ashx';
const HEADERS = {
  'Connection': 'keep-alive',
  'User-Agent': `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36`,
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  'Cookie': `gsScrollPos-863758127=0; gsScrollPos-863758124=0; gsScrollPos-863758121=0; gsScrollPos-863759583=0; \ 
gsScrollPos-863759587=0; gsScrollPos-863759739=0; gsScrollPos-863760439=; gsScrollPos-863760441=; \
gsScrollPos-863760440=; gsScrollPos-863760968=; gsScrollPos-863760442=; \
ASP.NET_SessionId=w2w5clftwoovx3dqrx5z5skk`,
  'Pragma': 'no-cache'
};

const start_urls = [{
  url: BASE_URL,
  qs: {
    class: 'AjaxTJCX',
    method: 'GetStatisticsResult',
    checkedName: '灾害总数,崩塌,塌陷,泥石流,地面沉降,地裂缝,滑坡,斜坡',
    queryLevel: '省',
    checkCodes: '000000',
    unit: 'PROVINCE'
  }
}];

start_urls.map(url => {
  console.log(`Start from site:\n${url.url}`);

  const options = {
    uri: url.url,
    qs: url.qs,
    json: true,
  }

  request(options)
    .then((response) => {
      return parse(response);
    }).then((result) => {
      // console.log(result);
      console.log('Parse Complete!')
    }).catch((err) => {
      if (err) { throw err }
    });
});

async function parse(response) {
  // write2File(response, {fileName: 'response.json'});

  const guid = response.guid;
  const rows = response.data ? response.data.rows : null;
  rows.filter((item) => {
    return item['省'];
  }).map((item) => {
    console.log(item);

    const qs ={
      guid,
      class: 'AjaxTJCX',
      method: 'FindStatisticsDetailInfo',
      districtName: '省',
      districtValue: item['省'],
      pageIndex: 0,
      pageSize: item['灾害总数'],
    };

    const options = {
      qs,
      HEADERS,
      uri: BASE_URL,
      json: true,
      timeout: 10 * 60 * 1000,
    }

    getDisasterPoints(options)
      .then(() => {
        console.log(`${item['省']} 共计 ${item['灾害总数']} 个地灾点`);
      }).catch((err) => {
        console.error(err);
      });
  });
}

async function getDisasterPoints(options) {
  try {
    const result = await request(options);
    write2File(result, {fileName: `${options.qs.districtValue}-地灾点.json`});
  } catch (error) {
    console.error(error);
  }
}

async function write2File(data, {fileName, directory='../data'}) {
  try {
    await fs.ensureDir(directory);
    fs.writeJson(`${directory}/${fileName}`, data);
  } catch (error) {
    console.error(error);
  }
}
